package com.cbgolf.oa.activity.permission;

import android.graphics.Matrix;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.cbgolf.oa.R;
import com.cbgolf.oa.adapter.MyPagerAdapter;
import com.cbgolf.oa.base.BaseActivity;
import com.cbgolf.oa.fragment.PermissionHistoryFragment;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/27.
 */

public class PermissionCheckHistoryActivity extends BaseActivity {
    //top
    @ViewInject(R.id.topBack_ll)
    private View topBack;
    @ViewInject(R.id.title_tv)
    private TextView topTitle;

    //body
    @ViewInject(R.id.permission_history_indicator_ll)
    private View indicatorView;
    @ViewInject(R.id.permission_history_all_ll)
    private View allView;
    @ViewInject(R.id.permission_history_allow_ll)
    private View allowView;
    @ViewInject(R.id.permission_history_refuse_ll)
    private View refuseView;
    //
    @ViewInject(R.id.permission_history_all_tv)
    private TextView allTv;
    @ViewInject(R.id.permission_history_allow_tv)
    private TextView allowTv;
    @ViewInject(R.id.permission_history_refuse_tv)
    private TextView refuseTv;
    //
    @ViewInject(R.id.permission_history_line_ll)
    private View lineView;
    @ViewInject(R.id.permission_history_line_iv)
    private ImageView lineIv;
    //viewpager
    @ViewInject(R.id.permission_history_viewpager)
    private ViewPager viewPager;
    //var
    private int currentIndex = 0;
    private int offSet = 0;

    private List<Fragment> listFragment = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a_permission_history);
    }

    @Override
    protected void findViews() {
        x.view().inject(this);

    }

    @Override
    protected void setViews() {
        topTitle.setText("审批历史");
        initLine();
        initViewPager();
    }

    private void initLine() {
        DisplayMetrics dm = new DisplayMetrics();
        getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
        offSet = dm.widthPixels / 3;
        Matrix matrix = new Matrix();
        matrix.postTranslate(offSet, 0);
        lineIv.setImageMatrix(matrix);
        int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        allTv.measure(w, h);
        int width = allTv.getMeasuredWidth();
        ViewGroup.LayoutParams vl = lineIv.getLayoutParams();
        vl.width = width;
//        vl.height = vl.WRAP_CONTENT;
        lineIv.setLayoutParams(vl);
        int padding = (offSet - width) / 2;
        lineView.setPadding(padding, 0, padding, 0);
    }

    PermissionHistoryFragment f_all;
    PermissionHistoryFragment f_allow;
    PermissionHistoryFragment f_refuse;

    private void initViewPager() {
        f_all = PermissionHistoryFragment.newInstance("");
        f_allow = PermissionHistoryFragment.newInstance(String.valueOf(true));
        f_refuse = PermissionHistoryFragment.newInstance(String.valueOf(false));
        listFragment.add(0, f_all);
        listFragment.add(1, f_allow);
        listFragment.add(2, f_refuse);
        MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager(), listFragment);
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(1);
        viewPager.setCurrentItem(0);
        f_all.onRefresh();

    }


    private final int ALL = 0;
    private final int ALLOW = ALL + 1;
    private final int REFUSH = ALLOW + 1;
    private boolean allowOnce = true;
    private boolean refuseOnce = true;

    @Override
    protected void setListener() {
        topBack.setOnClickListener(v -> finish());
        allView.setOnClickListener(v -> checkPage(ALL));
        allowView.setOnClickListener(v -> checkPage(ALLOW));
        refuseView.setOnClickListener(v -> checkPage(REFUSH));

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            int width = offSet;

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                Animation animation = new TranslateAnimation(currentIndex * width, position * width, 0, 0);
                currentIndex = position;
                animation.setFillAfter(true);
                animation.setDuration(150);
                lineIv.startAnimation(animation);
                checkPage(position);
                if (position == ALLOW && null != f_allow && allowOnce) {
                    f_allow.onRefresh();
                    allowOnce = false;
                } else if (position == REFUSH && null != f_refuse && refuseOnce) {
                    f_refuse.onRefresh();
                    refuseOnce = false;
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void checkPage(int index) {
        resetTextColor();
        switch (index) {
            case ALL:
                allTv.setTextColor(ContextCompat.getColor(context, R.color.bg_blue));
                break;
            case ALLOW:
                allowTv.setTextColor(ContextCompat.getColor(context, R.color.bg_blue));
                break;
            case REFUSH:
                refuseTv.setTextColor(ContextCompat.getColor(context, R.color.bg_blue));
                break;
            default:
                break;
        }
        viewPager.setCurrentItem(index, true);
    }

    private void resetTextColor() {
        allTv.setTextColor(ContextCompat.getColor(context, R.color.text_gray));
        allowTv.setTextColor(ContextCompat.getColor(context, R.color.text_gray));
        refuseTv.setTextColor(ContextCompat.getColor(context, R.color.text_gray));
    }
}
